package com.example.project;

import com.example.project.service.LoginService;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends Activity {
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		Button connexionButton = (Button)findViewById(R.id.connexionButton);
		connexionButton.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// Retrieve informations
				String address = ((EditText)findViewById(R.id.addressEditText)).getText().toString();
				String port = ((EditText)findViewById(R.id.portEditText)).getText().toString();
				String username = ((EditText)findViewById(R.id.usernameEditText)).getText().toString();
				String password = ((EditText)findViewById(R.id.passwordEditText)).getText().toString();
				
				// Call Login Service
				Intent loginServiceIntent = new Intent(getBaseContext(), LoginService.class);
				loginServiceIntent.putExtra("address", address);
				loginServiceIntent.putExtra("port", port);
				loginServiceIntent.putExtra("username", username);
				loginServiceIntent.putExtra("password", password);
				
				startService(loginServiceIntent);
				
//String uri = new StringBuffer("http://").append(address).append(':').append(port).append("/connect").toString();
//				
//				HttpClient httpClient = new DefaultHttpClient();
//				HttpContext localContext = new BasicHttpContext();
//				HttpPost httpPost = new HttpPost(uri);
//				
//				try {
//					List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
//					nameValuePairs.add(new BasicNameValuePair("username", username));
//					nameValuePairs.add(new BasicNameValuePair("password", password));
//					httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
//					
//					HttpResponse response = httpClient.execute(httpPost, localContext);
//					HttpEntity entity = response.getEntity();
//					
//					String token = StringUtils.inputStreamToString(entity.getContent());
//					System.out.println("CONTENT = "+token);
//					
//					Intent homeIntent = new Intent(getBaseContext(), HomeActivity.class);
//					homeIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//					homeIntent.putExtra("address", address);
//					homeIntent.putExtra("port", port);
//					homeIntent.putExtra("token", token);
//					getApplication().startActivity(homeIntent);
//
//				} catch (Exception e) {
//					System.out.println(e.getMessage());
//				}
			}
		});
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		return false;
	}

}
